// webpack.config.js
var path = require('path');
var webpack = require('webpack');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var HtmlWebpackPlugin = require('html-webpack-plugin');
var extend = require('./src/js/common/extend');
var env = (process.env.NODE_ENV || "dev");
var commonConfig = {
    entry: {
        common: ['./src/js/modules/common.js']
    },
    output: {
        path: 'dist',
        filename: 'js/modules/[name].bundle.js',
        publicPath: '../../',
    },
    // 新添加的module属性
    module: {
        loaders: [{
            test: /\.(jpg|png)$/,
            loader: "url-loader?limit=1000&mimetype=image/png&name=images/[name].[ext]"
        }, {
            test: /\.(html)$/,
            loader: "file-loader?/[path][name].[ext]"
        }, {
            test: /\.ejs$/,
            loader: 'ejs-loader'
        }]
    },
    bail:true,
};
var htmlWebpackPlugin = function(name, title) {
    return new HtmlWebpackPlugin({ //根据模板插入css/js等生成最终HTML
        title: title,
        filename: 'view/' + name + '.html', //生成的html存放路径，相对于 path
        template: './src/view/' + name + '.ejs', //html模板路径
        inject: 'head', //允许插件修改哪些内容，包括head与body
        minify: { //压缩HTML文件
            removeComments: true, //移除HTML中的注释
            collapseWhitespace: false //删除空白符与换行符
        },
        xhtml: true,
    });
}
var webpackConfig = [
    extend(true, {}, commonConfig, {
        entry: {
            selection: './src/js/modules/selection.js'
        },
        plugins: [
            htmlWebpackPlugin('selection/index', "主题精选"),
            htmlWebpackPlugin('selection/topics', "国企改革主题基金")
        ]
    }),
    extend(true, {}, commonConfig, {
        entry: {
            mine: './src/js/modules/mine.js'
        },
        plugins: [
            htmlWebpackPlugin('mine/index', "我的"),
            htmlWebpackPlugin('mine/message', "消息中心"),
            htmlWebpackPlugin('mine/my-account', "我的帐户"),
            htmlWebpackPlugin('mine/my-commision', "我的佣金"),
            htmlWebpackPlugin('mine/update-login-pwd', "修改登录密码"),
            htmlWebpackPlugin('mine/update-trade-pwd', "修改交易密码"),
            htmlWebpackPlugin('mine/about', "设置")
        ]

    }),
    extend(true, {}, commonConfig, {
        entry: {
            market: './src/js/modules/market.js'
        },
        plugins: [
            htmlWebpackPlugin('market/index', "产品超市"),
            htmlWebpackPlugin('market/add-bank-card1', '新增银行卡'),
            htmlWebpackPlugin('market/add-bank-card2', '新增银行卡'),
            htmlWebpackPlugin('market/certificate1', '实名认证'),
            htmlWebpackPlugin('market/certificate2', '实名认证'),
            htmlWebpackPlugin('market/certificate3', '实名认证'),
            htmlWebpackPlugin('market/certificate4', '实名认证'),
            htmlWebpackPlugin('market/choose-bank-card', '选择银行卡'),
            htmlWebpackPlugin('market/fee-rate', '基金超市'),
            htmlWebpackPlugin('market/fund-company', '基金超市'),
            htmlWebpackPlugin('market/fund-intro', '基金超市'),
            htmlWebpackPlugin('market/fund-manager', '基金超市'),
            htmlWebpackPlugin('market/fund-purchase', '基金申购'),
            htmlWebpackPlugin('market/fund-redemption', '基金赎回'),
            htmlWebpackPlugin('market/fund-rise', '基金超市'),
            htmlWebpackPlugin('market/history-profit', '基金超市'),
            htmlWebpackPlugin('market/history-value', '基金超市'),
            htmlWebpackPlugin('market/invest-strategy', '基金超市'),
            htmlWebpackPlugin('market/market-detail-currency', '基金超市'),
            htmlWebpackPlugin('market/market-detail-noncurrency', '基金超市'),
            htmlWebpackPlugin('market/market-list', '基金超市'),
            htmlWebpackPlugin('market/purchase-confirm', '申购确认'),
            htmlWebpackPlugin('market/purchase-result1', '申购结果'),
            htmlWebpackPlugin('market/purchase-result2', '申购结果'),
            htmlWebpackPlugin('market/purchase-result3', '申购结果'),
            htmlWebpackPlugin('market/redemption-confirm', '赎回确认'),
            htmlWebpackPlugin('market/redemption-result1', '赎回结果'),
            htmlWebpackPlugin('market/redemption-result2', '赎回结果'),
            htmlWebpackPlugin('market/search', '基金超市'),
        ]

    }),
    extend(true, {}, commonConfig, {
        entry: {
            asset: './src/js/modules/asset.js'
        },
        plugins: [
            htmlWebpackPlugin('asset/index', '我的资产'),
            htmlWebpackPlugin('asset/asset-structure', '持仓结构'),
            htmlWebpackPlugin('asset/position-detail', '持仓详情'),
            htmlWebpackPlugin('asset/position-detail-currency', '持仓详情'),
            htmlWebpackPlugin('asset/trade-detail', '交易记录详情'),
            htmlWebpackPlugin('asset/trade-records', '交易记录'),
        ]
    }),
    extend(true, {}, commonConfig, {
        entry: {
            account: './src/js/modules/account.js'
        },
        plugins: [
            htmlWebpackPlugin('account/forgot-login-pwd1', '忘记登录密码'),
            htmlWebpackPlugin('account/forgot-login-pwd2', '密码设置'),
            htmlWebpackPlugin('account/forgot-trade-pwd1', '忘记交易密码'),
            htmlWebpackPlugin('account/forgot-trade-pwd2', '身份核验'),
            htmlWebpackPlugin('account/forgot-trade-pwd3', '密码设置'),
            htmlWebpackPlugin('account/login', '登录'),
            htmlWebpackPlugin('account/register1', '注册'),
            htmlWebpackPlugin('account/register2', '手机号验证'),
            htmlWebpackPlugin('account/register3', '登录密码设置'),
            htmlWebpackPlugin('account/text-detail', '文本范例'),
        ]
    }),
];
for (var i = 0; i < webpackConfig.length; i++) {
    webpackConfig[i].plugins.push(new webpack.ProvidePlugin({
        ENV: path.resolve(__dirname, "src/env/" + env)
    }));
    webpackConfig[i].plugins.push(new webpack.optimize.CommonsChunkPlugin({
        names: ['common']
    }));

}
module.exports = webpackConfig;
